# Maintainer: He Yangxuan <yangxuan8282@gmail.com>

pkgname=linux-s905d
pkgver=4.18.7
case $pkgver in
*.*.*)	_kernver=${pkgver%.*};;
*.*)	_kernver=${pkgver};;
esac
pkgrel=6
pkgdesc="Linux kernel for Amlogic s905d"
url=https://github.com/150balbes/Amlogic_s905-kernel
_commit=0774525e710209a9e99136e5373b955f6c7defba
depends="mkinitfs"
_depends_dev="perl gmp-dev elfutils-dev bash"
makedepends="$_depends_dev sed installkernel bc linux-headers linux-firmware openssl1.0-dev bison flex"
options="!strip !check"
source="$url/archive/${_commit}.tar.gz
	config-changes-s905d.aarch64
	s905d_defconfig
	0001-arm64-dts-meson-gxl-add-support-for-phicomm-n1.patch
	brcmfmac-Disable-power-management.patch
	"
subpackages=""
arch="aarch64"
license="GPL-2.0"
_flavors=
for _i in $source; do
	case $_i in
	config-*.$CARCH)
		_f=${_i%.$CARCH}
		_f=${_f#config-changes-}
		_flavors="$_flavors ${_f}"
		[ "linux-$_f" != "$pkgname" ] && subpackages="$subpackages linux-${_f}::$CBUILD_ARCH"
		subpackages="$subpackages linux-${_f}-dev:_dev:$CBUILD_ARCH"
		;;
	esac
done

case "$CARCH" in
	aarch64) _carch="arm64" ;;
esac

HOSTCC="${CC:-gcc}"
HOSTCC="${HOSTCC#${CROSS_COMPILE}}"

prepare() {
	local _patch_failed=
	cd "$srcdir"/Amlogic_s905-kernel-$_commit

	for i in $source; do
		case $i in
		*.patch)
			msg "Applying $i..."
			if ! patch -s -p1 -N -i "$srcdir"/${i##*/}; then
				echo $i >>failed
				_patch_failed=1
			fi
			;;
		esac
	done

	if ! [ -z "$_patch_failed" ]; then
		error "The following patches failed:"
		cat failed
		return 1
	fi

	# remove localversion from patch if any
	rm -f localversion*

	local flavor=
	for flavor in $_flavors; do
		local builddir="$srcdir"/build-$flavor
		mkdir -p "$builddir"
		echo "-$pkgrel-$flavor" > "$builddir"/localversion-alpine
		_genconfig $flavor
		make -C "$srcdir"/Amlogic_s905-kernel-$_commit \
			O="$builddir" \
			ARCH="$_carch" \
			HOSTCC="$HOSTCC" \
			olddefconfig
		_verifyconfig $flavor
	done
}

# generate config from defconfig and apply local changes.
# config-changes-$flavor.$CARCH holds a list of = delimited
# config command and values used by kernel scripts/config script.
_genconfig() {
	local flavor=$1 defconfig=
	local builddir="$srcdir"/build-$flavor
	local defconfig=
	case $flavor in
		s905d) defconfig=s905d_defconfig ;;
		*) die "Unknown flavor: $flavor" ;;
	esac

	cp "$srcdir"/$defconfig \
		"$builddir"/.config

	while read line; do
		[ ${line:0:1} = "#" ] && continue
		local option=${line%%=*} str=
		local cmd=$(echo $line | cut -d= -f2)
		case "$cmd" in
			enable|disable|module) str= ;;
			set-str|set-val) str=${line##*=} ;;
			*) die "Command $cmd not accepted" ;;
		esac
		msg "[$flavor] $cmd: $option $str"
		"$srcdir"/Amlogic_s905-kernel-$_commit/scripts/config \
			--file "$builddir"/.config \
			--${cmd} "$option" "${str//\"/}"
	done < "$srcdir"/config-changes-$flavor.${CARCH}
}

# verify if options are set to correct value
_verifyconfig() {
	local flavor=$1
	local builddir="$srcdir"/build-$flavor
	while read line; do
		[ ${line:0:1} = "#" ] && continue
		local option=${line%%=*} str= invert=
		local cmd=$(echo $line | cut -d= -f2)
		case "$cmd" in
			enable) str="$option=y" ;;
			disable) str="$option"; invert="-v" ;;
			module) str="$option=m" ;;
			set-val) str="$option=${line##*=}" ;;
			set-str) str=${line##*=}
				str="$option=\"${str//\"/}\"" ;;
		esac
		grep -q $invert "^$str" "$builddir"/.config || \
			die "Config: $option not properly set!"
	done < "$srcdir"/config-changes-$flavor.${CARCH}
}

build() {
	unset LDFLAGS
	for i in $_flavors; do
		cd "$srcdir"/build-$i
		make ARCH="$_carch" CC="${CC:-gcc}" \
			KBUILD_BUILD_VERSION="$((pkgrel + 1 ))-Alpine"
	done
}

_package() {
	local _buildflavor="$1" _outdir="$2"
	local _abi_release=${pkgver}-${pkgrel}-${_buildflavor}

	cd "$srcdir"/build-$_buildflavor

	mkdir -p "$_outdir"/boot "$_outdir"/lib/modules

	local _install
	case "$CARCH" in
	arm*)
		_install="zinstall dtbs_install"
		;;
	aarch64)
		_install="install dtbs_install"
		;;
	*)
		_install=install
		;;
	esac

	cd "$srcdir"/build-$_buildflavor
	#local INSTALL_DTBS_PATH="$_outdir"/boot/dtbs
	local INSTALL_DTBS_PATH="$_outdir"/usr/lib/linux-${_abi_release}
	make -j1 modules_install $_install \
		ARCH="$_carch" \
		INSTALL_MOD_PATH="$_outdir" \
		INSTALL_PATH="$_outdir"/boot \
		INSTALL_DTBS_PATH="$INSTALL_DTBS_PATH"

	rm -f "$_outdir"/lib/modules/${_abi_release}/build \
		"$_outdir"/lib/modules/${_abi_release}/source
	rm -rf "$_outdir"/lib/firmware

	install -D include/config/kernel.release \
		"$_outdir"/usr/share/kernel/$_buildflavor/kernel.release

	if [ "$CARCH" = "aarch64" ]; then
		mv -f "$INSTALL_DTBS_PATH"/amlogic/*.dtb \
			"$INSTALL_DTBS_PATH"
		rmdir "$INSTALL_DTBS_PATH"/amlogic
	fi
}

# main flavor installs in $pkgdir
package() {
	depends="$depends linux-firmware-brcm"
	triggers="$pkgname.trigger=/usr/share/kernel/*"
	_package s905d "$pkgdir"
}

_dev() {
	local _flavor=$(echo $subpkgname | sed -E 's/(^linux-|-dev$)//g')
	local _abi_release=${pkgver}-${pkgrel}-$_flavor
	# copy the only the parts that we really need for build 3rd party
	# kernel modules and install those as /usr/src/linux-headers,
	# simlar to what ubuntu does
	#
	# this way you dont need to install the 300-400 kernel sources to
	# build a tiny kernel module
	#
	pkgdesc="Headers and script for third party modules for $_flavor kernel"
	depends="$_depends_dev"
	local dir="$subpkgdir"/usr/src/linux-headers-${_abi_release}

	# first we import config, run prepare to set up for building
	# external modules, and create the scripts
	mkdir -p "$dir"
	cp "$srcdir"/build-$_flavor/.config "$dir"/.config
	make -j1 -C "$srcdir"/Amlogic_s905-kernel-$_commit O="$dir" HOSTCC="${CC:-gcc}" \
		silentoldconfig prepare modules_prepare scripts

	# remove the stuff that points to real sources. we want 3rd party
	# modules to believe this is the soruces
	rm "$dir"/Makefile "$dir"/source

	# copy the needed stuff from real sources
	#
	# this is taken from ubuntu kernel build script
	# http://kernel.ubuntu.com/git/ubuntu/ubuntu-zesty.git/tree/debian/rules.d/3-binary-indep.mk
	cd "$srcdir"/Amlogic_s905-kernel-$_commit
	find .  -path './include/*' -prune \
		-o -path './scripts/*' -prune -o -type f \
		\( -name 'Makefile*' -o -name 'Kconfig*' -o -name 'Kbuild*' -o \
		   -name '*.sh' -o -name '*.pl' -o -name '*.lds' \) \
		-print | cpio -pdm "$dir"

	cp -a scripts include "$dir"
	find $(find arch -name include -type d -print) -type f \
		| cpio -pdm "$dir"

	install -Dm644 "$srcdir"/build-$_flavor/Module.symvers \
		"$dir"/Module.symvers

	mkdir -p "$subpkgdir"/lib/modules/${_abi_release}
	ln -sf /usr/src/linux-headers-${_abi_release} \
		"$subpkgdir"/lib/modules/${_abi_release}/build
}

sha512sums="6d7ae6e98eb42d824e0b33e402e4f71d6d76f47429e5df2f165ec1dbc2de26a07a4461d984bd18b8c1ea95b617240009a0ab1c974f2c49f4bae202a049abcac1  0774525e710209a9e99136e5373b955f6c7defba.tar.gz
5671f469f8db2983b2dc3537be9f21d94b680d02faadabfa0382f8b919ecb2226f3dac96934e426c30033673bedddea0cce7b734e1b7cc1f7e802af8cc78b2ab  config-changes-s905d.aarch64
2784f8c05c1c66f2bff73d0cdd415781e6529f269223607c18302803125092e7cb6c94083146deecc3226672f7f4754246cc62808f9384f697782ec55786883d  s905d_defconfig
2a430ef78f14e1037315c4292789fa3cff8a21da4f4a4df839842d8a54eae685b9feee0f2f63cb8e962052593785ea7dd67bd8ec055d25d1fc56a3a29856b50f  0001-arm64-dts-meson-gxl-add-support-for-phicomm-n1.patch
007075ca40915f20c6c8f5e04825c3433a52621deb689a9a690a3fcc618ca9629bc089a1a2ad5141fc47a8aa0aabb302ecf75e81ef8cae466e978074156231ac  brcmfmac-Disable-power-management.patch"
